<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表v-loading="listLoading"-->
    <el-table :data="tableDatas" v-loading="listLoading" @selection-change="handleSelectionChange" highlight-current-row
              style="width: 100%;">
      <!--多选框-->
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="70" label="序号">
      </el-table-column>
      <el-table-column prop="paramKey" label="参数键" width="120">
      </el-table-column>
      <el-table-column prop="paramValue" label="参数值" width="120">
        <template scope="scope">
          <div v-if="scope.row.type === 0">{{ scope.row.paramValue }}</div>
          <img v-else :src="scope.row.paramValue" alt="文件" style="max-width: 100px; max-height: 100px;">
        </template>
      </el-table-column>
      <el-table-column prop="type" label="参数类型" width="150">
        <template scope="scope">
          <div v-if="scope.row.type" style="color: red;">文件上传表单项</div>
          <div v-if="!scope.row.type" style="color: hotpink;">普通表达项</div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" width="120">
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" width="120">
      </el-table-column>
      <el-table-column prop="operatorName" label="操作人" width="120">
      </el-table-column>
      <el-table-column prop="paramDesc" label="参数描述"  show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" :disabled="this.sels.length === 0" @click="handlePatchDel">批量删除</el-button>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                     :page-sizes="[5, 10, 15]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="totals" style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加或修改界面-->
    <el-dialog title="保存用户" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm " label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="参数键" prop="paramKey">
          <el-input v-model="saveForm.paramKey" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="参数值" prop="paramValue">
          <template>
            <!-- 根据 saveForm.type 的值来决定显示文本框还是文件上传组件 -->
            <el-input v-if="saveForm.type === 0" v-model="saveForm.paramValue" placeholder="请输入参数值"></el-input>
            <el-upload
                v-else
                action="http://localhost:8080/minio"
                list-type="picture-card"
                name="file"
                :on-success="handleSuccess"
                :file-list="fileList"
                accept=".jpg,.jpeg,.png,.gif"
                :limit=1
                :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </template>
        </el-form-item>
        <el-form-item label="参数类型">
          <template>
            <el-radio v-model="saveForm.type" :label="0">普通类型</el-radio>
            <el-radio v-model="saveForm.type" :label="1">文件上传</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="操作人" prop="operatorName">
          <el-input v-model="saveForm.operatorName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="参数描述" prop="paramDesc">
          <el-input v-model="saveForm.paramDesc" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit('saveForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

export default {
  data() {
    return {
      fileList:[],
      //1.分页查询相关数据
      totals: 0,//总记录条数
      currentPage: 1,//当前页,要传递到后台的
      pageSize: 5, //每页显示多少条
      tableDatas: [], //当前页数据
      listLoading: false,
      dialogVisible: false,
      disabled: false,
      //2.高级查询相关数据
      keyword: '',

      //3.删除相关数据

      //4.批量删除相关数据
      sels: [], //存储多选记录

      //5.添加和修改相关数据
      saveFormVisible: false,
      saveForm: {
        id: null,
        paramKey:null,
        type:null,
        createTime:null,
        updateTime:null,
        operatorName:null,
        paramDesc:null,
        paramValue:null,
      },

      //6.添加和修改校验规则-表单项校验
      saveFormRules: {
        paramKey: [
          { required: true, message: '请输入参数键', trigger: 'blur' }
        ],
        paramValue: [
          { required: true, message: '请选择参数值', trigger: 'blur' },
        ],
        operatorName: [
          { required: true, message: '请输入操作人', trigger: 'blur' },
        ],
        paramDesc: [
          { required: true, message: '请输入参数描述', trigger: 'blur' },
        ]
      },
    }
  },
  methods: {
    // 如果为文件 则阻止上传行为
    beforeUpload(file) {
      this.selectedFile = file;
      return false;
    },
    //删除
    handleRemove(file) {
      console.log(file.data);
    },
    //上传成功后加载的方法
    handleSuccess(file,filelist) {
      console.log(file)
      // console.log(file.data);
      // this.saveForm.paramValue = file.data;
      console.log(filelist);
    },
    //1.分页查询相关方法
    //1.1.获取列表数据
    getTableData() {
      var data = {
        "page":this.currentPage,
        "size":this.pageSize,
        "keyword":this.keyword
      }
      this.$http.post("/param/page",data).then(res=>{
        console.log(res.data.data)
        if(res.data.success){
          this.tableDatas = res.data.data.list
          this.totals = res.data.data.total
        }else {
          this.$message(res.data.msg);
        }
      }).catch(res=>{
        console.log(res)
      })
    },
    //1.2.选择第几页时触发
    handleCurrentChange(curentPage) {
      this.currentPage = curentPage
      this.getTableData()
    },
    //1.3.选择每页显示记录条数时触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.getTableData()
    },

    //2.高级查询相关方法
    keywordQuery() {
      this.getTableData()
    },

    //3.删除相关方法
    handleDel(row){
      // console.log(row.id)
      let id = row.id
      this.$alert('确定要删除？', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if(action === 'confirm'){
            this.$http.delete("/param/"+id).then(res=>{
              if(res.data.success){
                this.getTableData()
                this.$message({
                  type: 'info',
                  message: `action: ${action}`
                });
              }else {
                this.$message(res.data.msg);
              }
              // console.log(res)
            }).catch(res=>{
              console.log(res)
            })
          }
        }
      })
    },


    //4.批量删除相关方法
    //4.1.选项改变时触发
    handleSelectionChange(val) {
      this.sels = val.map(a=>a.id)
      console.log(this.sels)
    },
    //4.2.批量删除
    handlePatchDel() {
      this.$alert('确定要删除'+this.sels+'吗?', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          if(action === 'confirm') {
            this.$http.post("/param/delBatch", this.sels).then(res => {
              // console.log(res)
              if (res.data.success) {
                this.getTableData()
                this.$message({
                  type: 'info',
                  message: `action: ${action}`
                })
              } else {
                this.$message(res.data.msg);
              }
            }).catch(res=>{
              console.log(res)
            })
          }
        }
      });
    },

    //5.添加和修改相关方法
    //5.1.点击添加弹出对话框
    handleAdd() {
      this.saveFormVisible = true
      this.saveForm = {
        type:0
      }
      //添加先清空
      this.fileList = []
    },
    //5.2.点击修改弹出对话框
    handleEdit(row) {
      this.saveFormVisible = true
      this.saveForm = Object.assign({}, row);
      //添加先清空
      this.fileList = [];
      var url = this.saveForm.paramValue
      var urlArr = url.split("/");
      // console.log(urlArr)
      var fileName = urlArr[urlArr.length-1]
      console.log(fileName)
      //存储到fileList中
      this.fileList.push({
        name:fileName,
        url:url
      })
    },
    //5.3.提交表单数据操作数据库
    saveSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if(valid){
          let formData = new FormData();
          formData.append('type', this.saveForm.type);
          if (this.saveForm.type === 0) {
            formData.append('paramValue', this.saveForm.paramValue);
          } else if (this.selectedFile) {
            formData.append('paramValue', this.selectedFile);
          }
          // var data1 = this.saveForm
          this.$http.post("/param",formData).then(res=>{
            // console.log(res)
            if(res.data.success){
              this.saveFormVisible = false
              this.getTableData()
            }else {
              this.$message(res.data.msg);
            }
          }).catch(res=>{
            console.log(res)
          })
        }
      })
    },

  },
  mounted() {
    this.getTableData();
  }
}

</script>

<style scoped>

</style>